<template>
  <el-dialog :visible.sync="dialogVisible" :title="title" width="500px" @callBack="onCallBack" @close="onClose">
    <el-form ref="ruleForm" :model="form" :rules="rules" label-width="80px" label-position="left">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" clearable disabled placeholder="请输入名称"/>
      </el-form-item>

      <el-form-item label="菜单选择" prop="menus">
        <el-tree
          ref="tree"
          :data="treeDate"
          show-checkbox
          :check-strictly="true"
          node-key="id"
          :default-checked-keys="checkIds"
          :props="defaultProps"
        >
        </el-tree>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>

  </el-dialog>
</template>

<script>
import { getOne, getCheckId, sysRoleDispense } from '@/api/sysRole'
import { getTree } from '@/api/sysPermission'

export default {
  data() {
    return {
      dialogVisible: false,
      title: '添加',
      treeDate: [],
      checkIds: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      form: {
        id: '',
        name: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入名称',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    init() {
      if (this.form.id) {
        getOne(this.form.id).then((res) => {
          if (res && res.code === 20000) {
            const rtn = res.data
            this.form.name = rtn.name
          }
        })
      }
    },
    onShow(id) {
      if (id) {
        this.form.id = id
        this.title = '编辑'
        this.init()
      } else {
        this.title = '添加'
      }
      this.dialogVisible = true
      this.getTreeDate()
    },
    onClose() {
      this.$refs['ruleForm'].resetFields()
      this.form = this.$options.data().form
      this.dialogVisible = false
    },
    onSubmit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          sysRoleDispense({
            'id': this.form.id,
            'sysPermissions': this.$refs['tree'].getCheckedNodes()
          }).then(res => {
            if (res && res.code === 20000) {
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.checkIds = []
              this.dialogVisible = false
              this.onCallBack()
            }
          })
        } else {
          return false
        }
      })
    },
    onCallBack() {
      this.$emit('onCallBack')
    },
    getTreeDate() {
      this.checkIds = []
      getTree().then(res => {
        if (res.code === 20000) {
          this.treeDate = res.data
          getCheckId(this.form.id).then(res => {
            if (res.code === 20000) {
              this.checkIds = res.data
            }
          })
        }
      })
    }
  },
  mounted() {
    // this.getTreeDate()
  }
}
</script>

<style scoped>
.el-form {
  padding-left: 60px;
  padding-right: 60px;
}
</style>
